<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="案例类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择案例类型" clearable>
          <el-option
            v-for="dict in dict.type.case_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-select v-model="queryParams.sort" clearable placeholder="请选择">
          <el-option
            v-for="dict in sortType"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="dr">
        <el-select v-model="queryParams.dr" placeholder="请选择">
          <el-option
            v-for="dict in dict.type.audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['information:wenzhang:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['information:wenzhang:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['information:wenzhang:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wenzhangList" @selection-change="handleSelectionChange" height="650" fixed>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="案例类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.case_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="封面" align="center" prop="coverResource" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.coverResource" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title"/>
      <el-table-column label="链接类型" align="center" prop="linkType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.link_type" :value="scope.row.linkType"/>
        </template>
      </el-table-column>
      <el-table-column label="摘要" align="center" prop="note"/>
      <el-table-column label="点击量" align="center" prop="num"/>
      <el-table-column label="发布时间" align="center" prop="createTime"/>
      <el-table-column label="来源" align="center" prop="from"/>
      <el-table-column label="作者" align="center" prop="createBy"/>
      <el-table-column label="审核状态" align="center" prop="dr">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.dr"/>
        </template>
      </el-table-column>
      <el-table-column width="220" fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleLook(scope.row.id)"
          >预览
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleRecom(scope.row)"
            v-hasPermi="['information:wenzhang:edit']"
            v-if="scope.row.state===0"
          >禁用
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleRecom(scope.row)"
            v-hasPermi="['information:wenzhang:edit']"
            v-if="scope.row.state===1"
          >启用
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleTop(scope.row)"
            v-hasPermi="['information:wenzhang:edit']"
            v-if="scope.row.top===1"
          >取消置顶
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleTop(scope.row)"
            v-hasPermi="['information:wenzhang:edit']"
            v-if="scope.row.top===0"
          >置顶
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['information:wenzhang:edit']"
          ><span v-if="$store.getters.userId===scope.row.sort">编辑</span>
            <span v-if="$store.getters.userId!==scope.row.sort">查看</span>
          </el-button>
          <el-button
            style=""
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="spaceAudit(scope.row)"
            v-if="scope.row.dr===0"
            v-hasPermi="['information:wenzhang:process']"
          >审核
          </el-button>
          <el-button
            style=""
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="spaceAudit(scope.row)"
            v-if="scope.row.dr===3"
            v-hasPermi="['information:wenzhang:process2']"
          >审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['information:wenzhang:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务案例对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal='false' width="759px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="标题" prop="title">
          <el-input class="kind" v-model="form.title" placeholder="请输入标题"/>
        </el-form-item>
        <el-form-item label="案例类型" prop="type">
          <el-select class="kind" v-model="form.type" placeholder="请选择案例类型">
            <el-option
              v-for="dict in dict.type.case_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="封面" prop="coverResource">
          <image-upload :limit="1" v-model="form.coverResource"/>
        </el-form-item>
        <el-form-item label="摘要" prop="note">
          <el-input class="kind" type="textarea" maxlength="100" show-word-limit v-model="form.note"  placeholder="请输入摘要"/>
        </el-form-item>
        <el-form-item label="来源" prop="from">
          <el-input class="kind" v-model="form.from" placeholder="来源"/>
        </el-form-item>
        <el-form-item label="作者" prop="createBy">
          <el-input class="kind" v-model="form.createBy" placeholder="请输入作者"/>
        </el-form-item>
        <el-form-item label="链接类型" prop="linkType">
          <el-select class="kind" @change="changeType" v-model="form.linkType" placeholder="请选择链接类型">
            <el-option
              v-for="dict in dict.type.link_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.linkType===1" label="内容">
          <MyEditor class="kind" v-model="form.linkVal" :min-height="192"/>
        </el-form-item>
        <el-form-item v-if="form.linkType===2" label="内容">
          <el-input class="kind" v-model="form.linkVal"/>
        </el-form-item>
        <el-form-item v-if="form.dr!=0" label="审核备注" prop="downloadUrl">
          <el-input disabled class="kind" type="textarea" v-model="form.downloadUrl"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" v-if="$store.getters.userId===form.sort" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 审核 -->
    <el-dialog title="审核" :visible.sync="buildingAudit" close-on-click-modal center width="750px">
      <el-form :model="form" label-width="130px">
        <el-form-item label="审核备注" prop="downloadUrl">
          <el-input class="kind" type="textarea" v-model="form.downloadUrl"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" v-if="form.dr===0" @click="submitCheck(3)">审核通过</el-button>
        <el-button type="success" v-if="form.dr===3" @click="submitCheck(1)">审核通过</el-button>
        <span style="width: 5%;display: inline-block"></span>
        <el-button type="danger" v-if="form.dr===0" @click="submitCheck(2)">审核驳回</el-button>
        <el-button type="danger" v-if="form.dr===3" @click="submitCheck(4)">审核驳回</el-button>
        <span style="width: 5%;display: inline-block"></span>
        <el-button type="info" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addWenzhang, delWenzhang, getWenzhang, listWenzhang, updateWenzhang} from "@/api/information/wenzhang";
import MyEditor from "@/components/wangEdiot/index.vue";

export default {
  name: "Wenzhang",
  components: {MyEditor},
  dicts: ['case_type', 'link_type','audit_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      buildingAudit: false,
      // 总条数
      total: 0,
      // 服务案例表格数据
      wenzhangList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        coverResource: null,
        title: null,
        linkType: null,
        downloadUrl: null,
        linkVal: null,
        note: null,
        sort: null,
        state: null,
        dr: null,
        num: null
      },
      // 表单参数
      form: {},
      sortType: [
        {
          value: 0,
          label: "按时间倒序"
        },
        {
          value: 1,
          label: "按时间正序"
        },
      ],
      // 表单校验
      rules: {
        type: [
          {required: true, message: "案例类型不能为空", trigger: "change"}
        ],
        coverResource: [
          {required: true, message: "封面不能为空", trigger: "blur"}
        ],
        title: [
          {required: true, message: "标题不能为空", trigger: "blur"},
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ],
        linkType: [
          {required: true, message: "链接类型不能为空", trigger: "change"}
        ],
        linkVal: [
          {required: true, message: "内容不能为空", trigger: "blur"}
        ],
        note: [
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ],
        createBy: [
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ],
        from: [
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleLook(id) {
      open(`http://120.46.220.152/#/pages/fwal/details?id=` + id)
    },
    changeType() {
      this.form.linkVal = null
    },
    /** 提交审核 */
    submitCheck(state) {
      updateWenzhang({
        id: this.form.id,
        dr: state,
        downloadUrl: this.form.downloadUrl
      }).then(response => {
        this.$modal.msgSuccess("审核成功");
        this.buildingAudit = false;
        this.getList();
      });
    },
    /** 审核 */
    spaceAudit(row) {
      this.reset();
      this.form.id = row.id
      this.form.dr = row.dr
      this.form.downloadUrl = row.downloadUrl
      this.buildingAudit = true;
    },
    /** 置顶按钮操作 */
    handleTop(row) {
      const id = row.id
      if (row.top === 0) {
        this.$modal.confirm('是否置顶编号为"' + id + '"的数据项？').then(function () {
          row.top = 1
          return updateWenzhang(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("置顶成功");
        }).catch(() => {
        });
      } else {
        this.$modal.confirm('是否取消置顶编号为"' + id + '"的数据项？').then(function () {
          row.top = 0
          return updateWenzhang(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("取消成功");
        }).catch(() => {
        });
      }

    },
    /** 启禁用按钮操作 */
    handleRecom(row) {
      const id = row.id
      if (row.state === 0) {
        this.$modal.confirm('是否确认禁用编号为"' + id + '"的数据项？').then(function () {
          row.state = 1
          return updateWenzhang(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("禁用成功");
        }).catch(() => {
        });
      } else {
        this.$modal.confirm('是否确认启用编号为"' + id + '"的数据项？').then(function () {
          row.state = 0
          return updateWenzhang(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("启用成功");
        }).catch(() => {
        });
      }

    },
    /** 查询服务案例列表 */
    getList() {
      this.loading = true;
      listWenzhang(this.queryParams).then(response => {
        this.wenzhangList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.buildingAudit = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        coverResource: null,
        title: null,
        linkType: null,
        downloadUrl: null,
        linkVal: null,
        note: null,
        sort: this.$store.getters.userId,
        state: null,
        dr: 0,
        createTime: null,
        updateTime: null,
        num: null,
        from: null,
        createBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.linkType = 1
      this.title = "添加服务案例";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWenzhang(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改服务案例";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.dr = 0
            this.form.downloadUrl = ''
            updateWenzhang(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWenzhang(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除服务案例编号为"' + ids + '"的数据项？').then(function () {
        return delWenzhang(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('information/wenzhang/export', {
        ...this.queryParams
      }, `wenzhang_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.kind {
  width: 90%;
}
</style>
